//
//  QuestionView.m
//  webview
//
//  Created by 蓓蕾 on 2022/2/26.
//

#import "QuestionView.h"

static const CGFloat TitleItemHeight = 50;

@interface QuestionView ()

@property (nonatomic, strong) UILabel *titleView;

@property (nonatomic, strong) UIView *titleBgView;

@end

@implementation QuestionView

-(id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        _selectSubAnswers = [NSMutableArray new];
        _titleBgView = [[UIView alloc] init];
        [self addSubview:_titleBgView];
        _titleBgView.backgroundColor = [UIColor orangeColor];
        [_titleBgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.mas_left).mas_offset(0);
            make.right.mas_equalTo(self.mas_right).mas_offset(0);
            make.top.mas_equalTo(self.mas_top).mas_offset(20);
            make.height.mas_equalTo(TitleItemHeight);
        }];
        
        _titleView = [[UILabel alloc] init];
        [_titleBgView addSubview:_titleView];
        _titleView.text = @"标题";
        _titleView.font = [UIFont systemFontOfSize:20];
        _titleView.textColor = [UIColor whiteColor];
        _titleView.textAlignment = NSTextAlignmentCenter;
        [_titleView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(_titleBgView.mas_left).mas_offset(0);
            make.right.mas_equalTo(_titleBgView.mas_right).mas_offset(0);
            make.centerY.mas_equalTo(_titleBgView.mas_centerY).mas_offset(0);
//            make.bottom.mas_equalTo(_titleBgView.mas_bottom).mas_offset(0);
        }];
        
        _subView = [[UIView alloc] init];
        [self addSubview:_subView];
        [_subView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.mas_left).mas_offset(0);
            make.right.mas_equalTo(self.mas_right).mas_offset(0);
            make.top.mas_equalTo(_titleBgView.mas_bottom).mas_offset(20);
            make.bottom.mas_equalTo(self.mas_bottom).mas_offset(0);
        }];
        
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureMethod:)];
        [_titleBgView addGestureRecognizer:tapGesture];
    }
    return self;
}

-(void)tapGestureMethod:(UITapGestureRecognizer *)gesture
{
    if (_level == 1) {
        
    }
    else if (_level == 2)
    {
        
    }
    
    if (self.SelectAnswerItemBlock) {
        self.SelectAnswerItemBlock(self);
    }
}

-(void)setTitleString:(NSString *)titleString
{
    _titleString = titleString;
    _titleView.text = titleString;
}

-(NSMutableArray *)selectSubAnswers
{
    if (!_selectSubAnswers) {
        _selectSubAnswers = [NSMutableArray new];
    }
    return _selectSubAnswers;
}

-(void)setSubAnswerInfo:(NSDictionary *)subAnswerInfo
{
    _subAnswerInfo = subAnswerInfo;
    for (UIView *subView in [_subView subviews]) {
        [subView removeFromSuperview];
    }
    if (!_isSelected) {
        UIView *lastView = [[UIView alloc] init];
        [_subView addSubview:lastView];
        [lastView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(_subView.mas_left).mas_offset(20);
            make.right.mas_equalTo(_subView.mas_right).mas_offset(-20);
            make.top.mas_equalTo(_subView.mas_top).mas_offset(0);
            make.height.mas_equalTo(0);
        }];
        [_subView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(lastView.mas_bottom).mas_offset(0);
        }];
        return;
    }
    NSArray *subAnswers = subAnswerInfo[@"options"];
    if ([subAnswers isKindOfClass:[NSArray class]] && subAnswers.count > 0) {
        UIView *lastView;
        for (NSInteger i = 0; i < subAnswers.count; ++i) {
            NSString *titleString = subAnswers[i];
            QuestionView *questionView = [[QuestionView alloc] init];
            [_subView addSubview:questionView];
            questionView.titleString = titleString;
            questionView.fatherView = self;
            questionView.selectBgColor = _selectBgColor ? _selectBgColor : [UIColor orangeColor];
            questionView.normalBgColor = _normalBgColor ? _normalBgColor : [UIColor whiteColor];
            questionView.selectTextColor = _selectTextColor ? _selectTextColor : [UIColor whiteColor];
            questionView.normalTextColor = _normalTextColor ? _normalTextColor : [UIColor blackColor];
            if (_optionItemCornerRadio > 0) {
                questionView.optionItemCornerRadio = _optionItemCornerRadio;
            }
            questionView.level = 2;
            if ([_selectSubAnswers containsObject:titleString]) {
                questionView.isSelected = YES;
            }
            else
            {
                questionView.isSelected = NO;
            }
            if (lastView) {
                [questionView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.mas_equalTo(_subView.mas_left).mas_offset(20);
                    make.right.mas_equalTo(_subView.mas_right).mas_offset(-20);
                    make.top.mas_equalTo(lastView.mas_bottom).mas_offset(0);
                }];
            }
            else
            {
                [questionView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.mas_equalTo(_subView.mas_left).mas_offset(20);
                    make.right.mas_equalTo(_subView.mas_right).mas_offset(-20);
                    make.top.mas_equalTo(_subView.mas_top).mas_offset(0);
                }];
            }
            lastView = questionView;
            questionView.SelectAnswerItemBlock = ^(QuestionView * _Nonnull objectView) {
                NSDictionary *answerInfo = objectView.fatherView.subAnswerInfo;
                NSLog(@"answerInfo = %@",answerInfo);
                NSString *titleString = objectView.titleString;
                NSInteger is_dx = [answerInfo[@"is_dx"] integerValue];
                NSInteger num = [answerInfo[@"is_dx"] integerValue];
                if (is_dx == 1) {//单选
                    for (QuestionView *currentQuestionView in [objectView.fatherView.subView subviews]) {
                        if (objectView == currentQuestionView) {
                            currentQuestionView.isSelected = YES;
                        }
                        else
                        {
                            currentQuestionView.isSelected = NO;
                        }
                    }
                    [objectView.fatherView.selectSubAnswers removeAllObjects];
                    [objectView.fatherView.selectSubAnswers addObject:titleString];
                }
                else
                {//多选
                    if (objectView.fatherView.selectSubAnswers.count >= num) {
                        
                        if (objectView.isSelected) {
                            objectView.isSelected = NO;
                            if ([objectView.fatherView.selectSubAnswers containsObject:titleString]) {
                                [objectView.fatherView.selectSubAnswers removeObject:titleString];
                            }
                        }
                        else
                        {
                            NSLog(@"最多选择 %ld 项",num);
                        }
                    }
                    else
                    {
                        if (objectView.isSelected) {
                            objectView.isSelected = NO;
                            if ([objectView.fatherView.selectSubAnswers containsObject:titleString]) {
                                [objectView.fatherView.selectSubAnswers removeObject:titleString];
                            }
                        }
                        else
                        {
                            objectView.isSelected = YES;
                            if (![objectView.fatherView.selectSubAnswers containsObject:titleString]) {
                                [objectView.fatherView.selectSubAnswers addObject:titleString];
                            }
                        }
                    }
                }
                
                
                
                
                
                
                
                
                
                
//                if (objectView.isSelected) {
//                    objectView.isSelected = NO;
//                }
//                else
//                {
//                    objectView.isSelected = YES;
//                }
//                if (objectView.isSelected && ![objectView.fatherView.selectSubAnswers containsObject:objectView.titleString]) {
//                    [objectView.fatherView.selectSubAnswers addObject:objectView.titleString];
//                }
//                else if(!objectView.isSelected && [objectView.fatherView.selectSubAnswers containsObject:objectView.titleString])
//                {
//                    [objectView.fatherView.selectSubAnswers removeObject:objectView.titleString];
//                }
//                else
//                {
//                    NSLog(@"不做任何操作");
//                }
                NSLog(@"%@ = %@",objectView.fatherView.titleString,objectView.fatherView.selectSubAnswers);
            };
        }
        if (lastView) {
            [_subView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.bottom.mas_equalTo(lastView.mas_bottom).mas_offset(0);
            }];
        }
    }
}

-(void)setIsSelected:(BOOL)isSelected
{
    _isSelected = isSelected;
    if (isSelected) {
        _titleBgView.backgroundColor = _selectBgColor;
        _titleView.textColor = _selectTextColor;
    }
    else
    {
        _titleBgView.backgroundColor = _normalBgColor;
        _titleView.textColor = _normalTextColor;
    }
    self.subAnswerInfo = _subAnswerInfo;
}

- (void)setOptionItemCornerRadio:(CGFloat)optionItemCornerRadio {
    _optionItemCornerRadio = optionItemCornerRadio;
    _titleBgView.layer.cornerRadius = optionItemCornerRadio;
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
